<?php
/**
 * 信息Service
 * Date: 2025/11/05
 */

namespace App\Services\Pc;

use App\Models\AdvertModel;
use App\Models\CategoryModel;
use App\Models\ConfigModel;
use App\Models\ContentModel;
use App\Models\LinkModel;
use App\Services\BaseService;

class InfoService extends BaseService
{
    /**
     * 基础信息
     * @date 2025/11/05
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        // config信息查询
        $configInfo = ConfigModel::query()->pluck('content', 'key')->toArray();

        // 分类列表查询
        $categoryList = CategoryModel::query()
            ->where('status', CategoryModel::STATUS_OPEN)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'asc')
            ->get(['id', 'pid', 'icon', 'name'])
            ->toArray();
        $categoryList = buildTree($categoryList, 'id');

        // 菜单查询
        $menuList = AdvertModel::query()
            ->where('type', AdvertModel::TYPE_MENU)
            ->where('status', AdvertModel::STATUS_OPEN)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'desc')
            ->get(['id', 'name', 'jump', 'jump_link'])
            ->toArray();

        // 广告查询
        $noticeList = AdvertModel::query()
            ->where('type', AdvertModel::TYPE_NOTICE)
            ->where('status', AdvertModel::STATUS_OPEN)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'desc')
            ->get(['id', 'name', 'jump', 'jump_link'])
            ->toArray();

        // 友情链接查询
        $linkList = LinkModel::query()
            ->where('status', LinkModel::STATUS_OPEN)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'desc')
            ->get(['id', 'name', 'jump_link'])
            ->toArray();

        return $this->success([
            'config_info' => $configInfo,
            'category_list' => $categoryList,
            'menu_list' => $menuList,
            'notice_list' => $noticeList,
            'link_list' => $linkList
        ]);
    }

    /**
     * 搜索列表
     * @date 2025/11/05
     * @param array $params 请求参数
     * @return \Illuminate\Http\JsonResponse
     */
    public function search($params)
    {
        $data = ContentModel::query()
            ->select(['id', 'logo', 'title', 'description'])
            ->where('status', ContentModel::STATUS_OPEN)
            ->when(isset($params['keywords']) && $params['keywords'] != '', function ($query) use ($params) {
                $query->where('title', 'like', '%' . $params['keywords'] . '%');
            })
            ->orderBy('content.id', 'desc')
            ->paginate($params['size'] ?? config('project.size'));

        return $this->success([
            'list' => $data->items(),
            'meta' => [
                'total' => $data->total(),
                'size'  => $data->perPage(),
                'page'  => $data->currentPage()
            ]
        ]);
    }
}
